package com.base.caselib;

import android.view.View;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.amap.api.location.AMapLocation;
import com.base.caselib.databinding.ActivityUpcaseBinding;
import com.base.caselib.presenter.UpCasePresenter;
import com.base.caselib.view.UpCaseView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.RoadEntity;
import com.base.commonlib.event.AddressEvent;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.TimePickUtil;
import com.base.commonlib.widget.SingleChooseDialog;
import com.base.commonlib.widget.SingleSearchDialog;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by weikailiang on 2019/11/1.
 */
@Route(path = "/upcase/add")
public class UpCaseActivity extends CBaseBarMvpActivity<UpCasePresenter> implements UpCaseView{
    private ActivityUpcaseBinding mBinding;
    private SimpleDateFormat mFormart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_upcase;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityUpcaseBinding) mRootBinding;
        mPresenter = new UpCasePresenter(this,this);
        mPresenter.start();
        setBackNavigationTip(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWaringDialog.show();
            }
        });
        setBarTitle("案件上报");
    }

    @Override
    protected void start() {
        mBinding.tvDate.setText(mFormart.format(new Date()));
        mBinding.ivDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickUtil.pickDateTime(mContext, mBinding.tvDate);
            }
        });
        mBinding.tvDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickUtil.pickDateTime(mContext, mBinding.tvDate);
            }
        });
        mBinding.ivRoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SingleChooseDialog<RoadEntity>(mContext, "请选择路段", mPresenter.mMMineRoads, new SingleChooseDialog.OnItemChooseListener() {

                    @Override
                    public void onItemChoose(Object o) {
                        mBinding.street.setText(o.toString());
                    }
                }).show();
            }
        });
        Disposable disposable = RxBus2.getInstance().doSubscribe(AddressEvent.class, new Consumer<AddressEvent>() {
            @Override
            public void accept(AddressEvent randomRefreshEvent) throws Exception {
                mBinding.address.setText(randomRefreshEvent.getAddress());
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);

        mBinding.ivMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/map/mapoi").navigation();
            }
        });
        mBinding.rgPType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_z) {
                    mBinding.llF.setVisibility(View.GONE);
                    mBinding.llZ.setVisibility(View.VISIBLE);
                } else {
                    mBinding.llF.setVisibility(View.VISIBLE);
                    mBinding.llZ.setVisibility(View.GONE);
                }
            }
        });

        mBinding.street.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SingleSearchDialog<RoadEntity>(mContext, "请搜索路段", mPresenter.mRoadEntities, new SingleSearchDialog.OnItemChooseListener() {
                    @Override
                    public void onItemChoose(Object o) {
                        mBinding.street.setText(o.toString());
                    }
                }).show();
            }
        });

        //联动




    }

    @Override
    public void onBackPressedSupport() {
        mWaringDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }

    @Override
    public void returnLocation(AMapLocation aMapLocation) {
        mBinding.address.setText(aMapLocation.getAddress());
    }
}
